<template>
  <div class="">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <el-card>
      <!-- 搜素 -->
      <el-row>
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getOrdersList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getOrdersList"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!--商品列表 -->
      <el-table v-loading="loading" :data="orderlist" border stripe>
        <el-table-column
          label="#"
          type="index"
          :index="index1"
        ></el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格(元)">
        </el-table-column>
        <el-table-column prop="pay_status" label="是否付款">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.pay_status === '1'" type="success"
              >已付款
            </el-tag>
            <el-tag v-else type="danger">未付款 </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"> </el-table-column>
        <el-table-column prop="update_time" label="下单时间">
          <template slot-scope="scope">{{
            scope.row.update_time | dateFormat
          }}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 修改 -->
            <el-tooltip
              :enterable="false"
              effect="dark"
              content="修改地址"
              placement="top"
            >
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                @click="editAddressVisible"
              ></el-button>
              ></el-tooltip
            >
            <!-- 物流 -->
            <el-tooltip
              :enterable="false"
              effect="dark"
              content="查看物流"
              placement="top"
            >
              <el-button
                type="warning"
                icon="el-icon-location"
                size="mini"
                @click="showProcess"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[2, 4, 6, 8]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :disabled="loading"
        background
      >
      </el-pagination>
    </el-card>
    <!-- 修改地址对话框 -->
    <el-dialog
      title="提示"
      :visible.sync="addressVisible"
      width="50%"
      @close="addressVisibleClose"
    >
      <el-form
        :model="addressForm"
        :rules="addressFormRules"
        ref="addressFormRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="city">
          <el-cascader
            v-model="addressForm.city"
            :options="cityData"
            :props="{ expandTrigger: 'hover' }"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="detailAdd">
          <el-input v-model="addressForm.detailAdd"></el-input> </el-form-item
      ></el-form>
      <span slot="footer">
        <el-button @click="addressVisible = false">取 消</el-button>
        <el-button type="primary" @click="addressVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 查看物流对话框 -->
    <!-- 修改地址对话框 -->
    <el-dialog title="查看物流" :visible.sync="processVisible" width="50%">
      <el-timeline>
        <el-timeline-item></el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityData from './citydata'
export default {
  data() {
    return {
      // 是否显示物流对话框
      processVisible: false,
      // 快递进度信息
      processList: [],
      queryInfo: {
        query: '',
        // 当前的页数
        pagenum: 1,
        // 当前每页显示的数量
        pagesize: 4,
      },
      total: 0,
      orderlist: [],
      loading: true,
      addressVisible: false,
      addressForm: {
        city: [],
        detailAdd: '',
      },
      addressFormRules: {
        city: [{ required: true, message: '请选择地址', trigger: 'blur' }],
        detailAdd: [
          { required: true, message: '请填写详细地址', trigger: 'blur' },
        ],
      },
      cityData,
    }
  },

  created() {
    this.getOrdersList()
  },

  methods: {
    async getOrdersList() {
      this.loading = true
      const { data: res } = await this.$http
        .get('orders', { params: this.queryInfo })
        .then((result) => {
          // 成功的回调
          // console.log('成功的回调')

          return result
        })
        .catch(() => {
          // 失败的回调
          // console.log('失败的回调')
          return Promise.reject('抛出错误')
        })
        .finally(() => {
          setTimeout(() => {
            this.loading = false
          }, 500)
          // console.log('成功或失败都会执行的回调')
        })
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单列表失败')
      }
      // console.log(res)
      this.orderlist = res.data.goods
      this.total = res.data.total
    },
    // 监听pagesize 改变的事件
    handleSizeChange(newSize) {
      // console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getOrdersList()
    },
    // 监听页码值改变的事件
    handleCurrentChange(newpage) {
      // console.log(newpage)
      this.queryInfo.pagenum = newpage
      this.getOrdersList()
    },
    getOrdersToFirstPage() {
      this.queryInfo.pagenum = 1
      this.getOrdersList()
    },
    editAddressVisible() {
      this.addressVisible = true
    },
    // 级联选择选项变化
    handleChange() {
      console.log(this.addressForm.city)
    },
    addressVisibleClose() {
      this.$refs.addressFormRef.resetFields()
    },
    index1(index) {
      if (this.queryInfo.pagenum === 1) {
        return index + 1
      }
      if (this.queryInfo.pagenum != 1) {
        return (
          (index += (this.queryInfo.pagenum - 1) * this.queryInfo.pagesize) + 1
        )
      }
    },
    // 显示物流
    async showProcess() {
      // const { data: res } = await this.$http.get('/kuaidi/1106975712662')
      // if (res.meta.status !== 200) {
      //   return this.$message.error(res.meta.msg)
      // }
      // console.log(res)
      // this.processList = res.data
      this.processVisible = true
    },
  },
}
</script>
<style lang="less" scoped>
@import '../../plugins/timeline/timeline.css';
@import '../../plugins/timeline-item/timeline-item.css';
.el-cascader {
  width: 100%;
}
</style>
